# encoding: UTF-8

class TaiSansController < ApplicationController
  # GET /tai_sans
  # GET /tai_sans.json
  def index
    @per_page = Param.get_value("itemsperpage")
    @search = TaiSan.where('TenTS LIKE ? OR ThongsoKT LIKE ?', "%#{params[:search]}%", "%#{params[:search]}%")
    @tai_sans = @search.paginate(:page => params[:page], :per_page => @per_page)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @tai_sans }
      format.csv { send_data TaiSan.to_csv(@search) }
      format.xls
    end
  end

  # GET /tai_sans/1
  # GET /tai_sans/1.json
  def show
    @tai_san = TaiSan.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @tai_san }
    end
  end

  # GET /tai_sans/new
  # GET /tai_sans/new.json
  def new
    if sign_in?
      @tai_san = TaiSan.new

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @tai_san }
      end
    else
      render 'sessions/deny'
    end
  end

  # GET /tai_sans/1/edit
  def edit
    if sign_in?
      @tai_san = TaiSan.find(params[:id])
    else
      render 'sessions/deny'
    end
  end

  # POST /tai_sans
  # POST /tai_sans.json
  def create
    @tai_san = TaiSan.new(params[:tai_san])

    respond_to do |format|
      if @tai_san.save
        format.html { redirect_to @tai_san, notice: 'Tai san was successfully created.' }
        format.json { render json: @tai_san, status: :created, location: @tai_san }
      else
        format.html { render action: "new" }
        format.json { render json: @tai_san.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /tai_sans/1
  # PUT /tai_sans/1.json
  def update
    @tai_san = TaiSan.find(params[:id])

    respond_to do |format|
      if @tai_san.update_attributes(params[:tai_san])
        format.html { redirect_to @tai_san, notice: 'Tai san da luu thanh cong!' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @tai_san.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /tai_sans/1
  # DELETE /tai_sans/1.json
  def destroy
    @tai_san = TaiSan.find(params[:id])
    @tai_san.destroy

    respond_to do |format|
      format.html { redirect_to tai_sans_url }
      format.json { head :no_content }
    end
  end

  def import
    if sign_in?
      @ts_import = TaiSan.new
      respond_to do |format|
        format.html
      end
    else
      render 'sessions/deny'
    end

  end

  # Tạo đường dẫn để import
  def save
    if (TaiSan.import(params[:file]))
      redirect_to import_tai_sans_path, notice: t("flash.import_msg")
    else
      redirect_to import_tai_sans_path, notice: t("flash.import_error")
    end
  end


  #POST /tai_sans/add_quantity?value=
  def add_quantity
    PhieuNhap.create(:tai_san_id => params[:tai_san_id], :nhan_vien_id => params[:nhan_vien_id], :ngay_nhap => params[:ngay_nhap], :trang_thai => params[:trang_thai], :so_luong => params[:so_luong]);

    ts = TaiSan.find(params[:tai_san_id])
    ts.SLuong = ts.SLuong.to_i + params[:so_luong].to_i
    ts.save!
  end

  def liquidated_goods
    @per_page = Param.get_value("itemsperpage")
    CtPhong.all.each do |lg|
      if ((Date.today() - localize(lg.nam_sd, :format => '%Y-%m-%d').to_date).to_i >= lg.tai_san.NamSD.to_i*365) then
        lg.tinh_trang = "Thanh lý"
        lg.save!
      end
    end

    @querry = CtPhong.where('tinh_trang = ?', "#{"Thanh lý"}")
    @liquidated_goods = @querry.paginate(:page => params[:page], :per_page => @per_page)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @liquidated_goods }
      format.csv { send_data TaiSan.to_csv(@liquidated_goods) }
      format.xls
    end
  end



end
